package ztc.service;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by ztc on 15-11-18.
 */
public class ConApi {

    static Map<String,String> all=new HashMap<String, String>();
    public static void main(String[] args) throws JSONException {
        String httpArg = "beijing";
        //String jsonResult = request(httpArg);
        String jsonstr="{\"HeWeather data service 3.0\":[{\"aqi\":{\"city\":{\"aqi\":\"86\",\"co\":\"2\",\"no2\":\"44\",\"o3\":\"2\",\"pm10\":\"0\",\"pm25\":\"63\",\"qlty\":\"良\",\"so2\":\"2\"}},\"basic\":{\"city\":\"北京\",\"cnty\":\"中国\",\"id\":\"CN101010100\",\"lat\":\"39.904000\",\"lon\":\"116.391000\",\"update\":{\"loc\":\"2015-11-19 19:53\",\"utc\":\"2015-11-19 11:53\"}},\"daily_forecast\":[{\"astro\":{\"sr\":\"07:03\",\"ss\":\"16:56\"},\"cond\":{\"code_d\":\"404\",\"code_n\":\"404\",\"txt_d\":\"雨夹雪\",\"txt_n\":\"雨夹雪\"},\"date\":\"2015-11-19\",\"hum\":\"81\",\"pcpn\":\"6.2\",\"pop\":\"94\",\"pres\":\"1025\",\"tmp\":{\"max\":\"5\",\"min\":\"1\"},\"vis\":\"9\",\"wind\":{\"deg\":\"108\",\"dir\":\"无持续风向\",\"sc\":\"微风\",\"spd\":\"19\"}},{\"astro\":{\"sr\":\"07:04\",\"ss\":\"16:55\"},\"cond\":{\"code_d\":\"404\",\"code_n\":\"401\",\"txt_d\":\"雨夹雪\",\"txt_n\":\"中雪\"},\"date\":\"2015-11-20\",\"hum\":\"80\",\"pcpn\":\"0.2\",\"pop\":\"80\",\"pres\":\"1027\",\"tmp\":{\"max\":\"3\",\"min\":\"0\"},\"vis\":\"10\",\"wind\":{\"deg\":\"94\",\"dir\":\"无持续风向\",\"sc\":\"微风\",\"spd\":\"19\"}},{\"astro\":{\"sr\":\"07:05\",\"ss\":\"16:54\"},\"cond\":{\"code_d\":\"402\",\"code_n\":\"401\",\"txt_d\":\"大雪\",\"txt_n\":\"中雪\"},\"date\":\"2015-11-21\",\"hum\":\"91\",\"pcpn\":\"5.0\",\"pop\":\"82\",\"pres\":\"1029\",\"tmp\":{\"max\":\"1\",\"min\":\"-1\"},\"vis\":\"9\",\"wind\":{\"deg\":\"91\",\"dir\":\"无持续风向\",\"sc\":\"微风\",\"spd\":\"19\"}},{\"astro\":{\"sr\":\"07:06\",\"ss\":\"16:54\"},\"cond\":{\"code_d\":\"402\",\"code_n\":\"400\",\"txt_d\":\"大雪\",\"txt_n\":\"小雪\"},\"date\":\"2015-11-22\",\"hum\":\"97\",\"pcpn\":\"23.7\",\"pop\":\"75\",\"pres\":\"1029\",\"tmp\":{\"max\":\"0\",\"min\":\"-2\"},\"vis\":\"2\",\"wind\":{\"deg\":\"88\",\"dir\":\"无持续风向\",\"sc\":\"微风\",\"spd\":\"19\"}},{\"astro\":{\"sr\":\"07:07\",\"ss\":\"16:53\"},\"cond\":{\"code_d\":\"104\",\"code_n\":\"101\",\"txt_d\":\"阴\",\"txt_n\":\"多云\"},\"date\":\"2015-11-23\",\"hum\":\"91\",\"pcpn\":\"0.2\",\"pop\":\"67\",\"pres\":\"1034\",\"tmp\":{\"max\":\"0\",\"min\":\"-6\"},\"vis\":\"10\",\"wind\":{\"deg\":\"132\",\"dir\":\"无持续风向\",\"sc\":\"微风\",\"spd\":\"19\"}},{\"astro\":{\"sr\":\"07:08\",\"ss\":\"16:53\"},\"cond\":{\"code_d\":\"101\",\"code_n\":\"101\",\"txt_d\":\"多云\",\"txt_n\":\"多云\"},\"date\":\"2015-11-24\",\"hum\":\"86\",\"pcpn\":\"0.0\",\"pop\":\"25\",\"pres\":\"1037\",\"tmp\":{\"max\":\"-1\",\"min\":\"-9\"},\"vis\":\"10\",\"wind\":{\"deg\":\"138\",\"dir\":\"北风\",\"sc\":\"3-4\",\"spd\":\"19\"}},{\"astro\":{\"sr\":\"07:09\",\"ss\":\"16:52\"},\"cond\":{\"code_d\":\"101\",\"code_n\":\"100\",\"txt_d\":\"多云\",\"txt_n\":\"晴\"},\"date\":\"2015-11-25\",\"hum\":\"80\",\"pcpn\":\"0.1\",\"pop\":\"1\",\"pres\":\"1036\",\"tmp\":{\"max\":\"-1\",\"min\":\"-10\"},\"vis\":\"10\",\"wind\":{\"deg\":\"131\",\"dir\":\"北风\",\"sc\":\"4-5\",\"spd\":\"19\"}}],\"hourly_forecast\":[{\"date\":\"2015-11-19 19:00\",\"hum\":\"85\",\"pop\":\"81\",\"pres\":\"1026\",\"tmp\":\"4\",\"wind\":{\"deg\":\"103\",\"dir\":\"东南风\",\"sc\":\"微风\",\"spd\":\"13\"}},{\"date\":\"2015-11-19 22:00\",\"hum\":\"86\",\"pop\":\"93\",\"pres\":\"1027\",\"tmp\":\"3\",\"wind\":{\"deg\":\"106\",\"dir\":\"东南风\",\"sc\":\"微风\",\"spd\":\"9\"}}],\"now\":{\"cond\":{\"code\":\"101\",\"txt\":\"多云\"},\"fl\":\"-4\",\"hum\":\"99\",\"pcpn\":\"0\",\"pres\":\"1027\",\"tmp\":\"2\",\"vis\":\"4\",\"wind\":{\"deg\":\"150\",\"dir\":\"东南风\",\"sc\":\"4-5\",\"spd\":\"19\"}},\"status\":\"ok\",\"suggestion\":{\"comf\":{\"brf\":\"较不舒适\",\"txt\":\"白天会有降雪发生，您会感觉偏冷，不很舒适，请注意添加衣物。\"},\"cw\":{\"brf\":\"不宜\",\"txt\":\"不宜洗车，未来24小时内有雪，如果在此期间洗车，雪水和路上的泥水可能会再次弄脏您的爱车。\"},\"drsg\":{\"brf\":\"冷\",\"txt\":\"天气冷，建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。\"},\"flu\":{\"brf\":\"较易发\",\"txt\":\"天气较凉，较易发生感冒，请适当增加衣服。体质较弱的朋友尤其应该注意防护。\"},\"sport\":{\"brf\":\"较不宜\",\"txt\":\"有降雪，推荐您在室内进行低强度运动；若坚持户外运动，请选择合适运动并注意保暖。\"},\"trav\":{\"brf\":\"较适宜\",\"txt\":\"有降雪，出行请注意携带雨具。微风，稍冷，但做好保暖措施还是可以出去欣赏雪景的。\"},\"uv\":{\"brf\":\"最弱\",\"txt\":\"属弱紫外线辐射天气，无需特别防护。若长期在户外，建议涂擦SPF在8-12之间的防晒护肤品。\"}}}]}";

        toMap("",jsonstr);

        System.out.print("Size："+all.size());
        /*
        Set set=all.keySet();
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            String key= (String) iterator.next();
            System.out.println(key+"::"+all.get(key));
        }*/

    }

    public static Map<String, String> getAll(String tatol) {
        toMap("", request(tatol));
        return all;
    }

    public static void clearAll() {
        all.clear();
    }

    public static void toMap(String exName,String jsonStr){
        try {
            JSONObject jO=new JSONObject(jsonStr);
            Iterator iterator=jO.keys();
            while(iterator.hasNext()){
                String key= (String) iterator.next();
                if(jO.get(key) instanceof JSONObject){
                        //System.out.println(((JSONObject) jO.get(key)).length());
                        toMap(exName + "_" + key, jO.get(key).toString());
                }else if(jO.get(key) instanceof JSONArray){
                    JSONArray jA=(JSONArray) jO.get(key);
                    for(int i=0;i<jA.length();i++){
                        toMap(exName+"_"+key,jA.get(i).toString());
                    }
                }else {
                    while(all.containsKey(exName+"_"+key))
                        exName+="*";
                    //System.out.println(exName+"_"+key+":"+jO.getString(key));
                    all.put(exName+"_"+key,jO.getString(key));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    /**
     * @param
     *
     * @param httpArg
     *            :参数
     * @return 返回结果
     */
    public static String request(String httpArg) {
        String httpUrl = "http://apis.baidu.com/heweather/weather/free?city=";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            // 填入apikey到HTTP header
            connection.setRequestProperty("apikey",  );
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
